package com.jinzhi.jzweb.controller;


import com.baomidou.mybatisplus.plugins.Page;
import com.jinzhi.common.annotation.Log;
import com.jinzhi.common.base.BaseController;
import com.jinzhi.common.utils.Result;
import com.jinzhi.jzweb.domain.MsgDO;
import com.jinzhi.jzweb.service.MsgService;
import com.jinzhi.sys.domain.RoleDO;
import com.jinzhi.sys.domain.UserDO;
import com.jinzhi.sys.domain.UserRoleDO;
import com.jinzhi.sys.service.RoleService;
import com.jinzhi.sys.service.UserRoleService;
import com.jinzhi.sys.service.UserService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * 
 * <pre>
 * 消息表
 * </pre>
 * <small> 2020-05-13 16:09:21 | lance</small>
 */
@Controller
@RequestMapping("/jzweb/msg")
public class MsgController extends BaseController {
	@Autowired
	private MsgService msgService;
	@Autowired
	private UserService userService;
	@Autowired
	private RoleService roleService;
	@Autowired
	private UserRoleService userRoleService;
	
	@GetMapping()
	@RequiresPermissions("jzweb:msg:msg")
	String Msg(){
	    return "jzweb/msg/msg";
	}
	
	@ResponseBody
	@GetMapping("/list")
	@RequiresPermissions("jzweb:msg:msg")
	public Result<Page<MsgDO>> list(MsgDO msgDTO){
		// 查询列表数据
		Page<MsgDO> page = msgService.selectPage(getPage(MsgDO.class),
					msgService.convertToEntityWrapper("title", msgDTO.getTitle() , "state" , msgDTO.getState() ,
							"type" , msgDTO.getType() , "msg_type" , msgDTO.getMsgType())
						.orderBy("id", false));
		List list = new ArrayList();
		for (int i = 0; i < page.getRecords().size(); i++) {
			MsgDO obj = (MsgDO) page.getRecords().get(i);

			UserDO user1 =  userService.selectById(obj.getUserId());
			if (user1 != null){
				obj.setUserName(user1.getUsername());
			}
			UserDO user2 = userService.selectById(obj.getCreaterId());
			if (user2 != null){
				obj.setCreaterName(user2.getUsername());
			}


			list.add(obj);
		}
		page.setRecords(list);
        return Result.ok(page);
	}
	
	@GetMapping("/add")
	@RequiresPermissions("jzweb:msg:add")
	String add(){
	    return "jzweb/msg/add";
	}

	@GetMapping("/edit/{id}")
	@RequiresPermissions("jzweb:msg:edit")
	String edit(@PathVariable("id") Long id,Model model){
		MsgDO msg = msgService.selectById(id);
		model.addAttribute("msg", msg);
	    return "jzweb/msg/edit";
	}
	
	@Log("添加消息表")
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("jzweb:msg:add")
	public Result<String> save( MsgDO msgDo , Long roleId){

		List<UserRoleDO> roleList = userRoleService.findByKv("roleId" , roleId);
		if (roleList.size() != 0){
			for (UserRoleDO userRole : roleList){
				MsgDO msg = new MsgDO();
				msg.setTitle(msgDo.getTitle());
				msg.setState(0);
				msg.setMsgType(msgDo.getMsgType());
				msg.setType(msgDo.getType());
				msg.setContent(msgDo.getContent());
				msg.setUserId(userRole.getUserId());
				msg.setCreaterId(this.getUserId());
				msg.setCreaterDate(new Date());
				msg.setEnclosure(msgDo.getEnclosure());
				msgService.insert(msg);
			}
		}
        return Result.ok();
	}
	
	@Log("修改消息表")
	@ResponseBody
	@RequestMapping("/update")
	@RequiresPermissions("jzweb:msg:edit")
	public Result<String>  update( MsgDO msg){
		boolean update = msgService.updateById(msg);
		return update ? Result.ok() : Result.fail();
	}
	
	@Log("删除消息表")
	@PostMapping( "/remove")
	@ResponseBody
	@RequiresPermissions("jzweb:msg:remove")
	public Result<String>  remove( Long id){
		msgService.deleteById(id);
        return Result.ok();
	}
	
	@Log("批量删除消息表")
	@PostMapping( "/batchRemove")
	@ResponseBody
	@RequiresPermissions("jzweb:msg:batchRemove")
	public Result<String>  remove(@RequestParam("ids[]") Long[] ids){
		msgService.deleteBatchIds(Arrays.asList(ids));
		return Result.ok();
	}

	@Log("用户角色列表")
	@ResponseBody
	@GetMapping("/roleList")
	Result<?> roleList(){
		List<RoleDO> roleList = roleService.findAll();
		return Result.ok(roleList);
	}
	
}
